<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手写sleep函数</title>
</head>
<body>
    <script>
        function sleep(ms){
            return new Promise((resolve,reject)=>{
                setTimeout(()=>{
                    // pending 等待
                    console.log(p);
                    resolve();
                    // fulfilled 已完成
                    console.log(p);
                },ms);
            })
        }
        // 测试
        sleep(2000).then(()=>{
            console.log("2秒后执行");
        }).catch(err=>{
            console.log(err);
        }).finally(()=>{
            // promise 状态从pending 到了 fulfilled 或 rejected 
            console.log("finally");
        })
    </script>
</body>
</html>